package com.kehutong.repair.dto;

import com.kehutong.common.entity.ITree;
import com.kehutong.common.entity.Root;
import org.coraframework.orm.Entity;
import org.coraframework.orm.annotation.Column;
import org.coraframework.orm.annotation.UnSave;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Department extends Entity implements ITree<Department> {

    private static final long serialVersionUID = -6907983875624176462L;

    protected Department(String id) {
        super(id);
        this.order = 30;
    }

    @Column(comment="归属公司")
    private String companyNo;

    @Column(comment="内部上级id")
    private String pid;

    @Column(comment="腾讯上级部门id")
    private String pcode;

    @Column(length=64, comment="上级部门名称")
    private String pname;

    @Column(comment="腾讯部门id")
    private String code;

    @Column(length=64, comment="部门名称")
    private String name;

    @Column(comment="排序")
    private int order;

    @Column(comment="逻辑删除")
    private boolean deleted;

//	@Column(comment = "第三方ID")
//	private String thirdId;


    @UnSave
    private List<Department> childList;


    public String getCompanyNo() {
        return companyNo;
    }

    public void setCompanyNo(String companyNo) {
        this.companyNo = companyNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPcode() {
        return pcode;
    }

    public void setPcode(String pcode) {
        this.pcode = pcode;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    @Override
    public List<Department> getChildList() {
        if (Objects.isNull(childList)) {
            return Collections.emptyList();
        }

        return childList;
    }

    @Override
    public void setChildList(List<Department> childList) {
        this.childList = childList;
    }

    public boolean isRoot() {
        return Root.isRoot(code);
    }

    @Override
    public void addChild(Department item) {
        if (childList == null) {
            childList = new ArrayList<>();
        }
        childList.add(item);
    }

//	public String getThirdId() {
//		return thirdId;
//	}
//
//	public void setThirdId(String thirdId) {
//		this.thirdId = thirdId;
//	}

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    @Override
    public int compareTo(Department o) {
        if (o.order > order) {
            return 1;
        }

        if (o.order < order) {
            return -1;
        }

        return  0;
    }
}
